package cn.dnui_jzy913.dnuifood_16110100913.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_jzy913.dnuifood_16110100913.Activity.FoodInfoActivity;
import cn.dnui_jzy913.dnuifood_16110100913.Activity.FoodListActivity;
import cn.dnui_jzy913.dnuifood_16110100913.R;
import cn.dnui_jzy913.dnuifood_16110100913.bean.Result;
import cn.dnui_jzy913.dnuifood_16110100913.bean.UserCollection;
import cn.dnui_jzy913.dnuifood_16110100913.common.Constants;
import cn.dnui_jzy913.dnuifood_16110100913.listener.RetrofitListener;
import cn.dnui_jzy913.dnuifood_16110100913.model.FoodModel;
import cn.dnui_jzy913.dnuifood_16110100913.model.UserModel;

public class Fragment_shopAdapter extends RecyclerViewAdapter<Fragment_shopAdapter.ViewHolder> implements RetrofitListener<Result> {
    private UserModel userModel;
    private FoodModel foodModel;
    private String shop_id,food_id,user_id;

    public Fragment_shopAdapter(Context context, List data, int layoutResource, String user_id) {
        super(context, data, layoutResource);
        this.user_id = user_id;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final UserCollection entity = (UserCollection) data.get(position);
        if (entity.getFlag().equals("0")){
            holder.res_name.setText("【"+entity.getShopname()+"】");
            holder.res_address.setText("地址:"+entity.getAddress());
            if (!entity.getPic().equals(""))
                Picasso.with(context).load(Constants.BASE_URL+entity.getPic()).into(holder.imageView);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //打开店铺详情页
                    Intent intent = new Intent(context,FoodListActivity.class);
                    intent.putExtra("shop_id",entity.getShop_id());
                    intent.putExtra("shop_name",entity.getShopname());
                    context.startActivity(intent);
                }
            });
            holder.imgBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //取消收藏店铺
                    uncollectShop(entity.getShop_id());
                }
            });
        }else {
            holder.foodname.setText(entity.getFoodname());
            holder.price.setText("￥"+entity.getPrice());
            if (!entity.getPic().equals(""))
                Picasso.with(context).load(Constants.BASE_URL+entity.getPic()).into(holder.imageView2);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //打开食品页面
                    Intent intent = new Intent(context,FoodInfoActivity.class);
                    intent.putExtra("food_id",entity.getFood_id());
                    intent.putExtra("phonenum","18742509781");
                    intent.putExtra("shop_name",entity.getShopname());
                    context.startActivity(intent);

                }
            });
            holder.imgBtn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    uncollectFood(entity.getFood_id());
                }
            });

        }
    }
    private void uncollectShop(String shop_id){   //取消收藏店铺
        this.shop_id = shop_id;
        foodModel = new FoodModel();
        foodModel.userCollectShop(user_id,shop_id,this);
    }
    private void uncollectFood(String food_id){   //取消收藏菜品
        this.food_id = food_id;
        userModel = new UserModel();
        userModel.userCollectFood(user_id,food_id,this);
    }


    @Override
    public void onSuccess(Result object, int flag) {
        switch (flag){
            case Constants.USERCOLLECTSHOP:
                if (object.getSuccess().equals("1")){
                    Toast.makeText(context,"取消收藏店铺成功",Toast.LENGTH_SHORT).show();
                    for (int i=0 ; i<data.size(); i++){
                        UserCollection bean = (UserCollection)data.get(i);
                        if (bean.getShop_id().equals(shop_id)){
                            data.remove(bean);
                            notifyDataSetChanged();//强制调用getView（）刷新item内容
                            break;
                        }
                    }
                }
                break;
            case Constants.USERCOLLECTFOOD:
                if (object.getSuccess().equals("1")){
                    Toast.makeText(context,"取消收藏菜品成功",Toast.LENGTH_SHORT).show();
                    for (int i = 0 ;i<data.size(); i++){
                        UserCollection bean = (UserCollection)data.get(i);
                        if (bean.getFood_id().equals(food_id)){
                            data.remove(bean);
                            notifyDataSetChanged();
                            break;
                        }
                    }
                }
                break;
        }
    }

    @Override
    public void onFalure() {
        Toast.makeText(context,"网路错误",Toast.LENGTH_LONG).show();
    }

    public static class  ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView,imageView2;
        TextView res_name,res_address,foodname,price;
        Button imgBtn,imgBtn2;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.scshop_res_image);
            res_name =itemView.findViewById(R.id.sc_shop_res_name);
            res_address = itemView.findViewById(R.id.sc_shop_res_address);
            imgBtn = itemView.findViewById(R.id.sc_shop_no);

            imageView2 = itemView.findViewById(R.id.sc_food_pic);
            foodname = itemView.findViewById(R.id.sc_food_foodname);
            price = itemView.findViewById(R.id.sc_food_price);
            imgBtn2 = itemView.findViewById(R.id.sc_food_no);

        }
    }
}
